/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Batch.Helpers;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.tida.servir.entities.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import org.hibernate.Session;

/**
 *
 * @author arson
 */
public class GeneracionPDF {

    public static final String ERROR_GENERANDO_EL_ARCHIVO = "Error generando el archivo: ";

    public InputStream generadoPDFConsultaSancionadosSinRegLab(List<LkBusquedaSancion> lcr, String path, Session session) {
        List<String> errores = new LinkedList<String>();
        Document document = new Document(PageSize.A4.rotate(), 50, 50, 100, 72);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            //PdfWriter writer = PdfWriter.getInstance(document, baos);
            PdfWriter.getInstance(document, new FileOutputStream(path));
            document.open();
            PdfPTable table = new PdfPTable(21);
            table.getHorizontalAlignment();
            table.setWidthPercentage(100);
            table.getDefaultCell().setBackgroundColor(BaseColor.LIGHT_GRAY);
            table.addCell(new Paragraph("Sancionado", FontFactory.getFont("arial", 10, Font.ITALIC, BaseColor.BLACK)));
            table.addCell(new Paragraph("Tipo Doc.Ide.", FontFactory.getFont("arial", 10, Font.ITALIC, BaseColor.BLACK)));
            table.addCell(new Paragraph("Num. Doc. Sancionado", FontFactory.getFont("arial", 10, Font.ITALIC, BaseColor.BLACK)));
            table.addCell(new Paragraph("Institución", FontFactory.getFont("arial", 10, Font.ITALIC, BaseColor.BLACK)));
            table.addCell(new Paragraph("Dirección Institución", FontFactory.getFont("arial", 10, Font.ITALIC, BaseColor.BLACK)));
            table.addCell(new Paragraph("Cargo", FontFactory.getFont("arial", 10, Font.ITALIC, BaseColor.BLACK)));
            table.addCell(new Paragraph("Tipo Doc.Not.", FontFactory.getFont("arial", 10, Font.ITALIC, BaseColor.BLACK)));
            table.addCell(new Paragraph("Nro.Doc.Not.", FontFactory.getFont("arial", 10, Font.ITALIC, BaseColor.BLACK)));
            table.addCell(new Paragraph("Fecha Doc.Not.", FontFactory.getFont("arial", 10, Font.ITALIC, BaseColor.BLACK)));
            table.addCell(new Paragraph("Tipo Doc.San.", FontFactory.getFont("arial", 10, Font.ITALIC, BaseColor.BLACK)));
            table.addCell(new Paragraph("Nro.Doc.San.", FontFactory.getFont("arial", 10, Font.ITALIC, BaseColor.BLACK)));
            table.addCell(new Paragraph("Fecha Doc.San.", FontFactory.getFont("arial", 10, Font.ITALIC, BaseColor.BLACK)));
            table.addCell(new Paragraph("Autoridad Sancionadora", FontFactory.getFont("arial", 10, Font.ITALIC, BaseColor.BLACK)));
            table.addCell(new Paragraph("Tipo Sancion", FontFactory.getFont("arial", 10, Font.ITALIC, BaseColor.BLACK)));
            table.addCell(new Paragraph("Estado", FontFactory.getFont("arial", 10, Font.ITALIC, BaseColor.BLACK)));
            table.addCell(new Paragraph("Inicio Inhabilitacion", FontFactory.getFont("arial", 10, Font.ITALIC, BaseColor.BLACK)));
            table.addCell(new Paragraph("Fin Inhabilitacion", FontFactory.getFont("arial", 10, Font.ITALIC, BaseColor.BLACK)));
            table.addCell(new Paragraph("Tiempo Restante", FontFactory.getFont("arial", 10, Font.ITALIC, BaseColor.BLACK)));
            table.addCell(new Paragraph("Causa Destitucion", FontFactory.getFont("arial", 10, Font.ITALIC, BaseColor.BLACK)));
            table.addCell(new Paragraph("Observaciones", FontFactory.getFont("arial", 10, Font.ITALIC, BaseColor.BLACK)));
            table.addCell(new Paragraph("Fecha Creacion", FontFactory.getFont("arial", 10, Font.ITALIC, BaseColor.BLACK)));
            table.getDefaultCell().setBackgroundColor(BaseColor.WHITE);

            for (int i = 0; i < lcr.size(); i++) {
                LkBusquedaSancion cr = (LkBusquedaSancion) lcr.get(i);
                if (cr.getEstrabajador() == true) {
                    table.addCell(leoCampo(cr.getB_datos_trabajador())); // Es un trabajador
                } else {
                    table.addCell(leoCampo(cr.getB_datos_persona())); // Es una persona
                }
                table.addCell(new Paragraph(leoCampo(cr.getTipo_doc_identidad()), FontFactory.getFont("arial", 10, Font.ITALIC, BaseColor.BLACK)));
                //table.addCell(leoCampo(cr.getTipo_doc_identidad()));
                if (cr.getEstrabajador() == true) {
                    table.addCell(leoCampo(cr.getNro_doc_trabajador()));  //Nro documento trabajador 
                } else {
                    table.addCell(leoCampo(cr.getNro_doc_persona())); //Nro docuento persona
                }
                table.addCell(leoCampo(cr.getRegimenlaboral()));
                table.addCell(leoCampo(cr.getTipo_sancion()));
                table.addCell(leoCampo(cr.getEstado()));
                table.addCell(cr.getTiempo_restante());
            }
            //generar
            File objFile = new File(path);
            FileOutputStream archivoSalida = new FileOutputStream(objFile);
            document.add(table);
        } catch (Exception e) {
            errores.add(ERROR_GENERANDO_EL_ARCHIVO + "xxxConsultaSancionados.pdf");
        }
        document.close();
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        return bais;
    }

    public static String leoCampo(String valor) {
        if (valor == null) {
            return "";
        }
        if ("".equals(valor.trim())) {
            return "";
        }
        return valor;
    }

    public static String datoAuxiliarToString(DatoAuxiliar valor) {
        if (valor == null) {
            return "";
        }
        return String.valueOf(valor.getCodigo());
    }

    private static String BoolFrom(Boolean estado) {
        if (estado != null) {
            if (estado) {
                return "1";
            } else {
                return "0";
            }
        }
        return "";
    }

    public static String datetoString(Date valor) {
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        if (valor == null) {
            return "";
        }
        return sdf.format(valor);
    }
}
